package com.karl.loading.loadings;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;

import com.karl.loading.base.BaseProgressView;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

/**
 * Created by Karl on 2017/8/28 0028.
 * desc:仿ios邮件应用下拉控件
 * <p>
 *     这里所说的线宽其实指的应该是每条矩形的宽
 * </p>
 */

public class IndicatorLoadingView  extends BaseProgressView{

    private Paint mPaint;

    private RectF mRectF;

    private int mLineWidth = 4;

    private int rotateValue = 0;

    private int progress;

    private LinkedList<Integer> mColorList = null;

    private boolean isRotating = false;

    public IndicatorLoadingView(Context context) {
        this(context,null);
    }

    public IndicatorLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        setMaxProgress(12);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(DEFAULT_COLOR);
        mPaint.setStyle(Paint.Style.FILL);

        mColorList = new LinkedList<>();
        mColorList.add(0xE7000000);
        mColorList.add(0xD2000000);
        mColorList.add(0xBD000000);
        mColorList.add(0xA8000000);
        mColorList.add(0x93000000);
        mColorList.add(0x7E000000);
        mColorList.add(0x69000000);
        mColorList.add(0x54000000);
        mColorList.add(0x3F000000);
        mColorList.add(0x2A000000);
        mColorList.add(0x15000000);
        mColorList.add(0x00000000);
        mLineWidth = dp2px(DEFAULT_STROKE_WIDTH);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth()/2;
        int y = getHeight()/2;
        
        int c = mLineWidth/2;
        
        canvas.save();
        if (mRectF == null){
            //用于画每一条线
            mRectF = new RectF(x-c,0,x+c,y/2);
        }

        switch (getCurrentMode()){
            case MODE_PROGRESS:
                mPaint.setColor(mColorList.get(0));
                for (int i = 0; i < progress; i++) {
                    canvas.drawRoundRect(mRectF,c,c,mPaint);
                    canvas.rotate(30,x,y);
                }
                break;
            case MODE_LOADING:
                for (int i = 0; i < getMaxProgress(); i++) {
                    mPaint.setColor(mColorList.get(i));
                    canvas.drawRoundRect(mRectF,c,c,mPaint);
                    canvas.rotate(30,x,y);

                }
                if (isRotating){
                    mColorList.addFirst(mColorList.getLast());
                    mColorList.removeLast();
                }
                break;
        }

        canvas.restore();

    }

    @Override
    public void updateProgress(int progress) {
        super.updateProgress(progress);
        if (progress <= getMaxProgress()){
            this.progress = progress;
            postInvalidate();
        }

    }

    @Override
    public void setColor(int color) {
        for (int i = 0; i < mColorList.size(); i++) {
            mColorList.set(i, getNewColor(mColorList.get(i), color));
        }
        postInvalidate();
    }

    public void setLineWidth(int width){
        this.mLineWidth = width;
        postInvalidate();
    }

    @Override
    protected ValueAnimator initAnim() {
        return ValueAnimator.ofInt(0,12);
    }

    @Override
    public void startAnim() {
        startAnim(500);
    }

    @Override
    public void startAnim(long time) {
        isRotating = true;
        startAnim(ValueAnimator.INFINITE,ValueAnimator.RESTART,time);
    }

    @Override
    protected void onUpdateAnim(ValueAnimator animator) {
        int value = (int) animator.getAnimatedValue();
        if (this.rotateValue != value){
            this.rotateValue = (int) animator.getAnimatedValue();
            postInvalidate();
        }
    }

    @Override
    public void stopAnim() {
        super.stopAnim();
        resetColorOrder();
    }

    /**
     * 重置color的排序
     */
    public void resetColorOrder() {
        Collections.sort(mColorList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //转换成16进制字符串，有000000ff的情况会被转成ff，所以加格式约束，空格用0代替
                String hexString1 = String.format("%8s", Integer.toHexString(o1)).replace(" ", "0");
                hexString1 = hexString1.substring(0, 2);
                String hexString2 = String.format("%8s", Integer.toHexString(o2)).replace(" ", "0");
                hexString2 = hexString2.substring(0, 2);

                //只比较16进制的前两位
                int hex1 = Integer.parseInt(hexString1, 16);
                int hex2 = Integer.parseInt(hexString2, 16);

                if (hex1 < hex2) {
                    return 1;
                } else if (hex1 > hex2) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        invalidate();
    }

    /**
     * 获取新的color
     *
     * @param oldColor 用于提取保存的alpha通道
     * @param color
     * @return
     */
    private int getNewColor(int oldColor, int color) {
        int newColor;
        int alpha = Math.round(Color.alpha(oldColor));
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        newColor = Color.argb(alpha, r, g, b);
        return newColor;
    }

}
